import Vue from 'vue'
import Vuex from 'vuex'

import { getUserInfo } from '@/api/user'

import { getToken, setToken, removeItem } from '@/utils/storage'
const tokenName = 'token'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    tokens: getToken(tokenName) || null,
    userinfo: {}
  },
  getters: {
  },
  mutations: {
    setUserToken (state, val) {
      state.tokens = val
      // localStorage.setItem('token', JSON.stringify(val))
      setToken(tokenName, val)
    },

    getUserToken (state) {

    },
    removeUserToken (state) {
      state.tokens = 'val'
      // localStorage.removeItem('token')
      removeItem(tokenName)
    },
    setUserInfo (state, val) {
      state.userinfo = val
    },
    removeUserInfo (state) {
      state.userinfo = {}
    }
  },
  actions: {
    async loadUserInfo (state) {
      try {
        const res = await getUserInfo()
        console.log(res)
        state.commit('setUserInfo', res.data.data)
      } catch (error) {
        console.log(error)
      }
    }
  },
  modules: {

  }
})
